package com.example.pingze.controller;

import com.example.pingze.entity.PoetInfo;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.PoetInfoService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/poetInfo")
@CrossOrigin(origins = "*")
public class PoetInfoController {
    @Autowired
    private PoetInfoService service;
    //通过诗人名称获取诗人全部信息
    @PostMapping("/getPoetInfo")
    public Result<PoetInfo> getAuthorInfo(@RequestBody PoetInfo poetInfo){
        if(poetInfo.getPoetName() == null){
            throw new PingZeException("诗人名称为空");
        }
        String name = poetInfo.getPoetName();
        PoetInfo find = service.findPoetInfoByPoetName(name);
        return ResultVOUtils.result(find);
    }

    /**
     * 通过诗人名字判断诗人信息在数据库中是否存在
     *
     * @param author
     * @return boolean
     */
    @PostMapping("/getStatus")
    public Result getStatus(@RequestParam String author){
        if (author == null){
            throw new PingZeException("未获取诗人名称");
        }
        PoetInfo poetInfo = service.findPoetInfoByPoetName(author);
        if (poetInfo == null){
            return ResultVOUtils.success("false");
        }else {
            return ResultVOUtils.success("true");
        }
    }

    /**
     * 后台管理系统
     */
    @CrossOrigin
    @PostMapping("/page")
    public Result<Page<PoetInfo>> getList(
            PoetInfo poet,
            @RequestParam(name = "pageCode", required = false, defaultValue = "1") Integer pageCode,
            @RequestParam(name = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ){
        if(pageCode < 1){
            throw new PingZeException("页码不能小于1");
        }
        Sort.Order nameOrder = Sort.Order.asc("name");
        Sort sort = Sort.by(nameOrder);
        Pageable pageable = PageRequest.of(pageCode - 1, pageSize, sort);
        Page page = service.getPoetInfoList(poet, pageable);
        return ResultVOUtils.result(page);
    }
    @CrossOrigin
    @PostMapping("/add")
    public Result<PoetInfo> add(String name, String headImageUrl, String simpleIntro){
        PoetInfo add = service.add(name, headImageUrl, simpleIntro);
        return ResultVOUtils.result(add);
    }
    @CrossOrigin
    @PostMapping("/edit")
    public Result<PoetInfo> edit(String id, String name, String headImageUrl, String simpleIntro){
        PoetInfo edit = service.edit(id, name, headImageUrl, simpleIntro);
        return ResultVOUtils.result(edit);
    }
    @CrossOrigin
    @Transactional
    @PostMapping("/delete")
    public Result<String> delete(String id){
        PoetInfo poetInfo = service.findPoetInfoById(id);
        if(poetInfo != null){
            service.delete(poetInfo);
        }
        return ResultVOUtils.success("delete success");
    }
}
